import requests
import json
import urllib.parse

#请求url，获取id号
def request_id(url,title):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36',
        'cookie': '_ga=GA1.3.1862801744.1582273520; zg_did=%7B%22did%22%3A%20%22177dc01a159cc-0e577b5de11199-73e356b-144000-177dc01a15a192%22%7D; zg_=%7B%22sid%22%3A%201614303830372%2C%22updated%22%3A%201614303870335%2C%22info%22%3A%201614303830381%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22ehall.nju.edu.cn%22%2C%22cuid%22%3A%20%22MF1914077%22%7D; search_history_yw_a=author%3D%26author_type1%3D0%26author_type2%3D1%26title%3D%25E7%259B%25AE%25E5%25BD%2595%26title_type1%3D1%26ywqk%3D%26ywqk_type1%3D1%26ywxj%3D%26ywnd1%3D1998%2C1999%2C2000%2C2001%2C2002%2C2003%2C2004%2C2005%2C2006%2C2007%2C2008%2C2009%2C2010%2C2011%2C2012%2C2013%2C2014%2C2015%2C2016%2C2017%2C2018%2C2019%2C2020%2C2021%26ywlx%3D%u8BF7%u9009%u62E9%26ywnd%3D%26search_model%3DAND%26order_type%3Dnum%26order_px%3DDESC%26pagesize%3D20; PHPSESSID=71jrcgflm77gd5fdr5mqfhsjh5'
    }
    values = {}
    a = title
    values['title'] = a
    aa = str(urllib.parse.urlencode(values)).strip('title=')
    years = {}
    years['years'] = '1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021'
    yy = str(urllib.parse.urlencode(years)).strip('years=')
    params = {
        'control': 'search_base',
        'action': 'search_ywsy',
        'author':'',
        'author_type1':'1',
        'author_type2':'1',
        'title':aa,
        'title_type1':'1',
        'ywqk':'',
        'ywqk_type1':'1',
        'ywxj':'',
        'ywnd':'',
        'ywnd1':yy,
        'ywnd2':'',
        'ywlx':'',
        'search_model':'AND',
        'order_type':'num',
        'order_px':'DESC',
        'pagesize':'20',
        'search_tag':'0',
        'session_key':'623',
        'pagenow':'1'
    }
    res = requests.get(url, params=params, headers=headers)
    res_json = res.json()
    if res_json['contents']:
        id = res_json['contents'][0]['id']
        znum = res_json['znum']
    else:
        id = ''
        znum = ''
    return id,znum

#获得引证文献的详细信息
def search_details_citations(url,id):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36',
        'cookie': '_ga=GA1.3.1862801744.1582273520; zg_did=%7B%22did%22%3A%20%22177dc01a159cc-0e577b5de11199-73e356b-144000-177dc01a15a192%22%7D; zg_=%7B%22sid%22%3A%201614303830372%2C%22updated%22%3A%201614303870335%2C%22info%22%3A%201614303830381%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22ehall.nju.edu.cn%22%2C%22cuid%22%3A%20%22MF1914077%22%7D; search_history_yw_a=author%3D%26author_type1%3D0%26author_type2%3D1%26title%3D%25E7%259B%25AE%25E5%25BD%2595%26title_type1%3D1%26ywqk%3D%26ywqk_type1%3D1%26ywxj%3D%26ywnd1%3D1998%2C1999%2C2000%2C2001%2C2002%2C2003%2C2004%2C2005%2C2006%2C2007%2C2008%2C2009%2C2010%2C2011%2C2012%2C2013%2C2014%2C2015%2C2016%2C2017%2C2018%2C2019%2C2020%2C2021%26ywlx%3D%u8BF7%u9009%u62E9%26ywnd%3D%26search_model%3DAND%26order_type%3Dnum%26order_px%3DDESC%26pagesize%3D20; PHPSESSID=71jrcgflm77gd5fdr5mqfhsjh5'
    }
    years = {}
    years['years'] = '1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021'
    yy = str(urllib.parse.urlencode(years)).strip('years=')
    params = {
        'control': 'search',
        'action': 'catation_id',
        'id':id,
        'ywnd':yy,
        'pagenow':'1'
    }
    res = requests.get(url, params=params, headers=headers)
    print(res.url)
    res_json = res.json()
    return res_json['contents_list']

if __name__ == '__main__':
    f = open(r'../cssci/19-中国图书馆学报.json', 'r', encoding='utf-8')
    g = open(r'yw-19-中国图书馆学报.json','a', encoding='utf-8')
    for i in f.readlines():
        jsonstr = json.loads(i)
        try:
            sno = jsonstr['contents'][0]['sno']
            lypm = jsonstr['contents'][0]['lypm']
            id = request_id('http://cssci.nju.edu.cn/control/controllers.php',lypm)
        except Exception as e:
            sno = ''
            lypm = ''
            id = ['']
        if id[0]:
            znum = id[1]
            res = search_details_citations('http://cssci.nju.edu.cn/control/controllers.php',id[0])
            yz_lists = res
            res_dic = {}
            res_dic['sno'] = sno
            res_dic['lymp'] = lypm
            res_dic['id'] = id[0]
            res_dic['znum'] = znum
            res_dic['yz_lists'] = yz_lists
            res_json = json.dumps(res_dic,ensure_ascii=False)
            print(res_json)
            g.write(res_json+'\n')
        else:
            res_dic = {}
            res_dic['sno'] = sno
            res_dic['lymp'] = lypm
            res_dic['id'] = ''
            res_dic['znum'] = ''
            res_dic['yz_lists'] = ''
            res_json = json.dumps(res_dic,ensure_ascii=False)
            print(res_json)
            g.write(res_json + '\n')
    f.close()
    g.close()